package gprc.mydemo;

import grpc.mydemo.*;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;

import java.util.Iterator;

public class GrpcClient {

    public static void main(String[] args) throws InterruptedException {
        ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost", 8899).usePlaintext().build();
        //同步
        StudentServiceGrpc.StudentServiceBlockingStub blockingStub = StudentServiceGrpc.newBlockingStub(managedChannel);
        //异步
        StudentServiceGrpc.StudentServiceStub studentServiceStub = StudentServiceGrpc.newStub(managedChannel);

        MyResponse myResponse = blockingStub.getRealNameByUsername(MeRequest.newBuilder().setUsername("zhangsan").build());
        System.out.println(myResponse.getRealname());

        System.out.println("-----------------------------------------------------------");

        Iterator<StudentResponse> studentByAge = blockingStub.getStudentByAge(StudentRequest.newBuilder().setAge(18).build());
        studentByAge.forEachRemaining(x->System.out.println("name:"+x.getName()+"\tage:"+x.getAge()+"\tcity:"+x.getCity()));

        System.out.println("-----------------------------------------------------------");

        /*StreamObserver<StudentResponseList> studentResponseListStreamObserver = new StreamObserver<StudentResponseList>() {

            @Override
            public void onNext(StudentResponseList value) {
                value.getStudetnResponseList().forEach(x->{
                    System.out.println("name:"+x.getName()+"\tage:"+x.getAge()+"\tcity:"+x.getCity());
                });
            }

            @Override
            public void onError(Throwable throwable) {
                System.err.println(throwable.getMessage());
            }

            @Override
            public void onCompleted() {
                System.out.println("completed!");
            }
        };

        StreamObserver<StudentRequest> streamObserver = studentServiceStub.getStudentsWrapperByAges(studentResponseListStreamObserver);

        streamObserver.onNext(StudentRequest.newBuilder().setAge(20).build());
        streamObserver.onNext(StudentRequest.newBuilder().setAge(30).build());
        streamObserver.onNext(StudentRequest.newBuilder().setAge(40).build());

        streamObserver.onCompleted();*/

        System.out.println("-----------------------------------------------------------");

        StreamObserver<StreamResponse> result = new StreamObserver<StreamResponse>() {
            @Override
            public void onNext(StreamResponse streamResponse) {
                System.out.println("client:" + streamResponse.getResponseInfo());
            }

            @Override
            public void onError(Throwable throwable) {
                System.err.println(throwable.getMessage());
            }

            @Override
            public void onCompleted() {
                System.out.println("completed!");
            }
        };

        StreamObserver<StreamRquest> streamRquestStreamObserver = studentServiceStub.biTalk(result);

        streamRquestStreamObserver.onNext(StreamRquest.newBuilder().setRquesetInfo("客户端的第一条消息").build());
        streamRquestStreamObserver.onNext(StreamRquest.newBuilder().setRquesetInfo("客户端的第二条消息").build());
        streamRquestStreamObserver.onNext(StreamRquest.newBuilder().setRquesetInfo("客户端的第三条消息").build());

        streamRquestStreamObserver.onCompleted();

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
